package follow.juc.heima.day8;

import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;

public class Demo3Volatile {
    public static void main(String[] args) {
        VolatileDemo demo = new VolatileDemo();
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(demo);
            String name = t.getName();
            System.out.println(name);
            t.start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(demo.count);

    }

    static class VolatileDemo implements Runnable {
        public volatile int count;


        private ReentrantLock lock = new ReentrantLock();

        @Override
        public void run() {
            addCount();
        }

        public void addCount() {
            for (int i = 0; i < 10000; i++) {
                lock.lock();
                count++;
                lock.unlock();
            }
        }
    }
}
